package es.mgil.whoWantsToBeAMillonaire.handlers;

import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import es.mgil.whoWantsToBeAMillonaire.R;
import es.mgil.whoWantsToBeAMillonaire.activities.Play;
import es.mgil.whoWantsToBeAMillonaire.dialogs.FinishDialog;
import es.mgil.whoWantsToBeAMillonaire.dialogs.ShouldContinueDialog;
import es.mgil.whoWantsToBeAMillonaire.dialogs.WinnerDialog;
import es.mgil.whoWantsToBeAMillonaire.runnables.ResponseProgressBarRunnable;
import es.mgil.whoWantsToBeAMillonaire.util.MusicManager;
import es.mgil.whoWantsToBeAMillonaire.util.PreferencesKeys;

public class ResponseProgressBarHandler extends Handler {
	private Play play;

	public ResponseProgressBarHandler(Play play) {
		this.play = play;
	}

	@Override
	public void handleMessage(Message msg) {
		try {
			switch (msg.what) {
			case ResponseProgressBarRunnable.RESPONSE_PROGRESS_BAR_UPDATE:
				// simply update the progress bar
				if (play.progressBar != null
						&& play.progressBar.getProgress() < play.progressBar
								.getMax()) {
					int i = play.progressBar.getProgress() + 1;
					play.progressBar.setProgress(i);
					play.editor.putInt(
							PreferencesKeys.PREFERENCES_PROGRESS_BAR_PROGRESS,
							i);
					play.editor.commit();
				}
				break;
			case ResponseProgressBarRunnable.RESPONSE_PROGRESS_BAR_TIMEOUT:
				// the progress bar arrives to its maximum, so user looses the
				// game
				Toast.makeText(play, R.string.play_timeout, Toast.LENGTH_SHORT)
						.show();
				play.editor.putInt(PreferencesKeys.PREFERENCES_FINAL_SCORE,
						play.getFinalAward());
				play.editor.commit();
				play.refreshAllAnswersState();

				if (MusicManager.isMusicEnabled(play.preferences)) {
					// is music enabled, start playing the loose music
					play.mmBack.stopPlayer();
					play.mmFailQuestion.startPlayer(play);
				} else {
					// else, show directly the finish dialog
					FinishDialog dialog1 = new FinishDialog();
					dialog1.show(play.getSupportFragmentManager(),
							FinishDialog.FINISH_DIALOG_ID);
				}
				break;
			case ResponseProgressBarRunnable.RESPONSE_PROGRESS_BAR_FAIL:
				// bad answer has been selected
				Toast.makeText(play, R.string.play_wrong, Toast.LENGTH_SHORT)
						.show();
				play.editor.putInt(PreferencesKeys.PREFERENCES_FINAL_SCORE,
						play.getFinalAward());
				play.editor.commit();
				play.refreshAllAnswersState();

				if (MusicManager.isMusicEnabled(play.preferences)) {
					// if music enabled, start the fail music
					play.mmBack.stopPlayer();
					play.mmFailQuestion.startPlayer(play);
				} else {
					// else, show directly the finish dialog
					FinishDialog dialog2 = new FinishDialog();
					dialog2.show(play.getSupportFragmentManager(),
							FinishDialog.FINISH_DIALOG_ID);
				}
				break;
			case ResponseProgressBarRunnable.RESPONSE_PROGRESS_BAR_CORRECT_NEXT:
				// correct answer has been selected
				Toast.makeText(play, R.string.play_correct, Toast.LENGTH_SHORT)
						.show();
				play.refreshAllAnswersState();

				if (MusicManager.isMusicEnabled(play.preferences)) {
					// if music enabled, start the winner question music
					play.mmBack.stopPlayer();
					play.mmWinQuestion.startPlayer(play);
				} else {
					// else, show directly the should continue dialog
					ShouldContinueDialog dialog3 = new ShouldContinueDialog();
					dialog3.show(play.getSupportFragmentManager(),
							ShouldContinueDialog.CONTINUE_DIALOG_ID);
				}
				break;
			case ResponseProgressBarRunnable.RESPONSE_PROGRESS_BAR_CORRECT_END:
				// final answer has been answeres correctly
				Toast.makeText(play, R.string.play_correct, Toast.LENGTH_SHORT)
						.show();
				play.refreshAllAnswersState();

				if (MusicManager.isMusicEnabled(play.preferences)) {
					// if music enabled, start final winner music
					play.mmBack.stopPlayer();
					play.mmWinGame.startPlayer(play);
				}

				// and start the winner dialog too
				WinnerDialog dialog4 = new WinnerDialog();
				dialog4.show(play.getSupportFragmentManager(),
						WinnerDialog.WINNER_DIALOG_ID);
				break;
			default:
				break;
			}
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}
}